package org.xtext.example.generator;

import com.google.common.collect.Iterators;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.xtext.example.astra.Agent;
import org.xtext.example.astra.PlainObject;
import org.xtext.example.generator.GenUtils;

@SuppressWarnings("all")
public class AgentToJava {
  private GenUtils util;
  
  private List<Agent> agentList;
  
  public void genJavaCode(final GenUtils utils) {
    this.util = utils;
    Resource _resource = this.util.getResource();
    TreeIterator<EObject> _allContents = _resource.getAllContents();
    Iterator<Agent> _filter = Iterators.<Agent>filter(_allContents, Agent.class);
    List<Agent> _list = IteratorExtensions.<Agent>toList(_filter);
    this.agentList = _list;
    this.genAgentSystem();
  }
  
  /**
   * Agents
   */
  public void genAgents() {
    for (final Agent agent : this.agentList) {
      {
        String _name = agent.getName();
        final String name = StringExtensions.toFirstUpper(_name);
        CharSequence _genLogo = this.util.genLogo();
        String _plus = ("" + _genLogo);
        String _genAgent = this.genAgent(agent);
        String _plus_1 = (_plus + _genAgent);
        this.util.genFile(name, "java", _plus_1);
      }
    }
  }
  
  public String genAgent(final Agent agent) {
    String _xblockexpression = null;
    {
      String outS = "";
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("public class ");
      String _name = agent.getName();
      String _firstUpper = StringExtensions.toFirstUpper(_name);
      _builder.append(_firstUpper, "");
      _builder.append(" extends Thread{");
      _builder.newLineIfNotEmpty();
      String _plus = (outS + _builder.toString());
      outS = _plus;
      EList<PlainObject> _resources = agent.getResources();
      for (final PlainObject resource : _resources) {
        CharSequence _genUseResource = this.genUseResource(resource);
        String _plus_1 = (outS + _genUseResource);
        outS = _plus_1;
      }
      StringConcatenation _builder_1 = new StringConcatenation();
      _builder_1.append("public void run(){");
      _builder_1.newLine();
      _builder_1.append("\t");
      _builder_1.append("System.out.println(\"Hello world from ");
      String _name_1 = agent.getName();
      _builder_1.append(_name_1, "	");
      _builder_1.append("\");");
      _builder_1.newLineIfNotEmpty();
      _builder_1.append("}");
      _builder_1.newLine();
      _builder_1.append("}");
      _builder_1.newLine();
      String _plus_2 = (outS + _builder_1.toString());
      outS = _plus_2;
      _xblockexpression = (outS);
    }
    return _xblockexpression;
  }
  
  /**
   * Features
   */
  public CharSequence genUseResource(final PlainObject obj) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("protected ");
    String _name = obj.getName();
    _builder.append(_name, "");
    _builder.append(" ");
    String _name_1 = obj.getName();
    _builder.append(_name_1, "");
    _builder.append("_Instance ;");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  /**
   * AgentSystem
   */
  public void genAgentSystem() {
    this.genAgents();
    String _genMain = this.genMain();
    String _plus = ("" + _genMain);
    this.util.genFile("Main", "java", _plus);
  }
  
  public String genMain() {
    String _xblockexpression = null;
    {
      String outS = "";
      CharSequence _genLogo = this.util.genLogo();
      String _plus = (outS + _genLogo);
      outS = _plus;
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("public class Main {");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("public static void main(String[] args){");
      _builder.newLine();
      String _plus_1 = (outS + _builder.toString());
      outS = _plus_1;
      String _genAgentInstances = this.genAgentInstances();
      String _plus_2 = (outS + _genAgentInstances);
      outS = _plus_2;
      StringConcatenation _builder_1 = new StringConcatenation();
      _builder_1.append("\t");
      _builder_1.append("}");
      _builder_1.newLine();
      _builder_1.append("}");
      _builder_1.newLine();
      String _plus_3 = (outS + _builder_1.toString());
      outS = _plus_3;
      _xblockexpression = (outS);
    }
    return _xblockexpression;
  }
  
  public String genAgentInstances() {
    String _xblockexpression = null;
    {
      String outS = "";
      for (final Agent agent : this.agentList) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append(" ");
        _builder.append("new ");
        String _name = agent.getName();
        String _firstUpper = StringExtensions.toFirstUpper(_name);
        _builder.append(_firstUpper, " ");
        _builder.append("().start();");
        _builder.newLineIfNotEmpty();
        String _plus = (outS + _builder.toString());
        outS = _plus;
      }
      _xblockexpression = (outS);
    }
    return _xblockexpression;
  }
}
